// pages/mine/loginuser/loginuser.js
Page({

  data: {nickName:'',
    avatarUrl:'',last:0,pid:'',id:'',flag:''

  },

  onLoad: function (options) {
    if(options.pid){
      this.setData({pid:options.pid})
    }
    if(options.id){
      this.setData({id:options.id})
    }
    console.log(this.data.id,this.data.pid);
    

    
       
  },
  //获取用户信息 登陆
  getUserProfile(){
    wx.getUserProfile({
      desc:'111',
      success:(res)=>{
        const avatarUrl=res.userInfo.avatarUrl
        const nickName=res.userInfo.nickName
        console.log(res);
        wx.login({
          success:function(e){
          console.log(e);
          const code=e.code
          wx.request({
            url: 'https://zujieba.com/api/wxlogin/getOpenid.html',
            method:'POST',
            header: {
             'content-type': 'application/json'
         },
            data:{js_code:code},
      
            success:(result)=>{
              console.log(result);
              
              const openid=result.data.data.openid
              const session_key=result.data.data.session_key
              const type=result.data.data.type
              //获取用户信息
          
                 
             
               
               
               wx.setStorageSync('nickName', nickName)
               wx.setStorageSync('avatarUrl', avatarUrl)
               console.log(avatarUrl,nickName,openid,type);
               that.setData({
                 nickName,
                 avatarUrl
               })
               
               //获取手机加密信息
               const params={img:avatarUrl,username:nickName,openid,type}
               console.log(avatarUrl,nickName,openid,type);
               
                wx.request({
                  url: 'https://zujieba.com/Api/wxlogin/login.html',
                  data:params,
                  method:"POST",  
                  success:(res1)=>{
                    wx.hideLoading()
                    console.log(res1);
                  
                    
                    if(that.data.id!=''&&res1.data.success==1){
                    wx.request({
                      url:'https://zujieba.com/Api/bind/bind.html',
                      data:{pid:that.data.id,uid:res1.data.data.uid},
                      method:"POST",
                      success:(res)=>{
                        if(res.data.success==1){
                           wx.showToast({title:'绑定成功'})
                        }
                      }
                    })}
                    
                  if (res1.data.success==2){
                    wx.hideLoading()
                    that.throttle(()=>{
                      wx.navigateTo({
                        url: '../../mine/login/login?avatarUrl='+avatarUrl+"&nickName="+nickName+"&openid="+openid+"&type="+type+'&pid='+that.data.pid+"&id="+that.data.id
                      
                    })
                    },1500)
                   
                  }else{
                    wx.hideLoading()
                    console.log(res1);
                    wx.setStorageSync('uid', res1.data.data.uid)
                    wx.setStorageSync('avatarUrl', avatarUrl)
                    wx.setStorageSync('nickName', nickName)
                    wx.setStorageSync('phone', res1.data.data.phone)
                    if(that.data.flag==15){
                      wx.navigateBack()
                    }else{       var pages = getCurrentPages()   
                      　　 var currentPage = pages[pages.length - 1] 
                      　　var prevPage = pages[pages.length - 2]    
                      　　
                      　　prevPage.setData({
                      　　　avatarUrl,
                             nickName,
                            loginflag:true})
                                  wx.navigateBack({
                                    complete: (res) => {},
                                  })}
             
                  }
                    
                   
                    
                   } })}})}})
      },
      fail:(res)=>{
        console.log(res);
        
      }
    })
    wx.showLoading({
      title:'登录中'
    })
    const that=this
  },
 //节流
    throttle:function(func,wait){
      console.log(func,wait);
      
      let last =this.data.last
      console.log(last);
      
      let current_time= +new Date()
      if(current_time-last>wait){
        console.log(1111);
          console.log(current_time,current_time-last);
          
          func()
          last=+new Date()
          this.setData({
            last
          })
      }
        
         
      
       
    },
    cancel(){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
            
  
})